package com.quanshu.shop.person.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.person.adapter.AddressBaseAdapter;
import com.quanshu.shop.person.entity.AddressBaseEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者  李哲
 * 创建时间 2018/8/2
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class AdressPickerDialog extends Dialog {
    @BindView(R.id.address_dialog_fl_back)
    FrameLayout addressDialogFlBack;
    @BindView(R.id.address_dialog_rg)
    RadioGroup addressDialogRg;
    @BindView(R.id.address_dialog_fl)
    RecyclerView addressDialogFl;
    @BindView(R.id.address_dialog_rb_province)
    RadioButton addressDialogRbProvince;
    @BindView(R.id.address_dialog_rb_city)
    RadioButton addressDialogRbCity;
    @BindView(R.id.address_dialog_rb_area)
    RadioButton addressDialogRbArea;
    private Context mContext;
    private OnGetAddressListener onGetAddressListener;
    private AddressBaseAdapter addressBaseAdapter;
    private int state = -1;
    private List<AddressBaseEntity.DataEntity> addressProvinceList;
    private List<AddressBaseEntity.DataEntity> addressCityList;
    private List<AddressBaseEntity.DataEntity> addressAreaList;
    private AddressBaseEntity.DataEntity dataProvinceEntity;
    private AddressBaseEntity.DataEntity dataCityEntity;
    private AddressBaseEntity.DataEntity dataAreaEntity;

    public AdressPickerDialog(Context context) {
        super(context, R.style.CartDialogWindow);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address_picker);
        ButterKnife.bind(this);
        setCancelable(false);//点击外部不可dismiss
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = BaseTool.getAndroiodScreenProperty(mContext) / 4 * 3;
        window.setAttributes(params);
        initView();
    }

    private void initView() {
        RxViewHelper.getInstance().clicks(addressDialogFlBack, aVoid ->
                dismiss()
        );
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        addressDialogFl.setLayoutManager(linearLayoutManager);
        addressDialogRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.address_dialog_rb_province:
                        state = 1;
                        if (addressProvinceList != null && addressBaseAdapter != null) {
                            addressBaseAdapter.setNewData(addressProvinceList);
                        }
                        break;
                    case R.id.address_dialog_rb_city:
                        state = 2;
                        if (addressCityList != null && addressBaseAdapter != null) {
                            addressBaseAdapter.setNewData(addressCityList);
                        }
                        break;
                    case R.id.address_dialog_rb_area:
                        state = 3;
                        if (addressAreaList != null && addressBaseAdapter != null) {
                            addressBaseAdapter.setNewData(addressAreaList);
                        }
                        break;
                }
            }
        });
        getPrivince();
    }

    private void getPrivince() {
        NetClient.getInstance().getShopService(ShopMainIF.class).getProvince("").compose(new BaseTransformer<>((BaseActivity) mContext)).subscribe(new BaseObserver<BaseResponse<List<AddressBaseEntity.DataEntity>>>() {
            @Override
            public void onSuccess(BaseResponse<List<AddressBaseEntity.DataEntity>> response) {
                addressProvinceList = response.getData();
                addressBaseAdapter = new AddressBaseAdapter(addressProvinceList);
                state = 1;
                addressBaseAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId()) {
                            case R.id.base_address_txt:
                                if (state == 1) {
                                    dataProvinceEntity = addressProvinceList.get(position);
                                    addressDialogRbProvince.setText(addressProvinceList.get(position).getArea_name());
                                    //请求市的数据
                                    getCity(2, addressProvinceList.get(position).getArea_id());
                                    addressDialogRbCity.setText("");
                                    addressDialogRbArea.setText("");

                                } else if (state == 2) {
                                    dataCityEntity = addressCityList.get(position);
                                    addressDialogRbCity.setText(addressCityList.get(position).getArea_name());
                                    //请求区的数据
                                    getCity(3, addressCityList.get(position).getArea_id());
                                    addressDialogRbArea.setText("");

                                } else if (state == 3) {
                                    dataAreaEntity = addressAreaList.get(position);
                                    addressDialogRbArea.setText(addressAreaList.get(position).getArea_name());
                                    if (onGetAddressListener != null) {
                                        onGetAddressListener.selectAddress(dataProvinceEntity, dataCityEntity, dataAreaEntity);
                                        dismiss();
                                    }
                                }
                                break;
                        }
                    }
                });
                addressDialogFl.setAdapter(addressBaseAdapter);
            }
        });
    }

    private void getCity(int position, String Id) {
        NetClient.getInstance().getShopService(ShopMainIF.class).getCity(Id).compose(new BaseTransformer<>((BaseActivity) mContext)).subscribe(new BaseObserver<BaseResponse<List<AddressBaseEntity.DataEntity>>>() {
            @Override
            public void onSuccess(BaseResponse<List<AddressBaseEntity.DataEntity>> response) {
                if (position == 2) {
                    //请求后的区的数据
                    state = 2;
                    addressCityList = response.getData();
                    addressBaseAdapter.setNewData(addressCityList);
                    addressDialogRbCity.setText("请选择市");
                    addressDialogRbCity.setChecked(true);

                } else if (position == 3) {
                    //请求区的数据
                    state = 3;
                    addressAreaList = response.getData();
                    addressBaseAdapter.setNewData(addressAreaList);
                    addressDialogRbArea.setText("请选择区");
                    addressDialogRbArea.setChecked(true);
                }

            }
        });
    }

    public void setOnGetAddressListener(OnGetAddressListener onGetAddressListener1) {
        onGetAddressListener = onGetAddressListener1;
    }

    public interface OnGetAddressListener {
        void selectAddress(AddressBaseEntity.DataEntity province, AddressBaseEntity.DataEntity city, AddressBaseEntity.DataEntity area);
    }
}
